﻿#include "ConfigCommand.h"

#include <fast_io/fast_io_device.h>

#include "../../../Memory/GameData.h"
#include "../../Config/ConfigManager.h"

bool ConfigCommand_onExecute(const fast_io::vector<std::u8string>& args) {
	using std::operator""sv;
	if (args.size() > 3 || args.size() < 2)
		return false;
	else if (args.size() == 2) {
		if (args[1] == u8"list"sv) {

			fast_io::vector<std::u8string> configsString{ConfigManager::list()};

			C_GuiData::displayClientMessageC(Utils::TextColor::GRAY, u8"========================");
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														   u8"[Helium] ",
														   Utils::TextColor::YELLOW,
														   u8"Config List:");
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														   u8"[Helium] ",
														   Utils::TextColor::YELLOW,
														   u8"配置列表：");
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														   u8"[Helium] ",
														   Utils::TextColor::YELLOW,
														   u8"配置列表：");
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														   u8"[Helium] ",
														   Utils::TextColor::YELLOW,
														   u8"構成リスト：");
				break;
				std::unreachable();
			}

			for (auto& i : configsString)
				C_GuiData::displayClientMessageC(Utils::TextColor::GRAY, i);
			return true;
		} else if (args[1] == u8"save"sv) {
			if (configMgr.saveConfig()) {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"Successfully saved config: ",
													 configMgr.currentConfig);
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"保存配置 ",
													 configMgr.currentConfig,
													 u8" 成功！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"保存配置 ",
													 configMgr.currentConfig,
													 u8" 成功！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"構成 ",
													 configMgr.currentConfig,
													 u8" を正常に保存しました！");
					break;
					std::unreachable();

				}
				return true;
			} else {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"Failed to save config: ",
													 configMgr.currentConfig);
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"保存配置 ",
													 configMgr.currentConfig,
													 u8" 失败！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"保存配置 ",
													 configMgr.currentConfig,
													 u8" 失敗！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"構成 ",
													 configMgr.currentConfig,
													 u8" を保存できませんでした！");
					break;
					std::unreachable();
				}
				return true;
			}
			return true;
		} else {
			return false;
		}
	} else {
		if (args[1] == u8"load"sv) {
			if (args[2] == configMgr.currentConfig) {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::YELLOW,
													 u8"Config ",
													 args[2],
													 u8" has been loaded!");
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::YELLOW,
													 u8"配置 ",
													 args[2],
													 u8" 已经加载！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::YELLOW,
													 u8"配置 ",
													 args[2],
													 u8" 已經加載！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::YELLOW,
													 u8"構成 ",
													 args[2],
													 u8" がロードされました！");
					break;
					std::unreachable();
				}
				return true;
			}
			for (auto const& ent : recursive(at(configMgr.configDirectory))) {
				std::u8string_view view(u8extension(ent));
				if (view == u8".hcfg"sv) {
					std::u8string name_view(std::u8string_view(u8filename(ent)));
					name_view.resize(name_view.find(u8".hcfg"sv));
					if (args[2] == name_view) {
						auto tempdrt = drt(ent);
						if (configMgr.loadConfig(name_view, &tempdrt)) {
							switch (SettingManager::languageget) {
							case 0:
							default:
								C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
																 u8"[Helium] ",
																 Utils::TextColor::GREEN,
																 u8"Successfully loaded config: ",
																 args[2]);
								break;
							case 1:
								C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
																 u8"[Helium] ",
																 Utils::TextColor::GREEN,
																 u8"成功加载配置： ",
																 args[2]);
								break;
							case 2:
								C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
																 u8"[Helium] ",
																 Utils::TextColor::GREEN,
																 u8"成功加載配置： ",
																 args[2]);
								break;
							case 3:
								C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
																 u8"[Helium] ",
																 Utils::TextColor::GREEN,
																 u8"構成 ",
																 args[2],
																 u8" が正常にロードされました！");
								break;
								std::unreachable();
							}
							return true;
						} else {
							break;
						}
					}
				}
			}
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"Failed to load config: ",
												 args[2]);
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"加载配置 ",
												 args[2],
												 u8" 失败！");
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"加載配置 ",
												 args[2],
												 u8" 失敗！");
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"構成 ",
												 args[2],
												 u8" の読み込みに失敗しました！");
				break;
				std::unreachable();
			}
			return true;
		} else if (args[1] == u8"create"sv) {
			fast_io::vector<std::u8string> configsString{ConfigManager::list()};
			if (args[2] == configMgr.currentConfig || std::ranges::any_of(configsString, [&args](std::u8string& al) { return args[2] == al; })) {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"Config ",
													 args[2],
													 u8" already exists!");
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"配置 ",
													 args[2],
													 u8" 已经存在！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"配置 ",
													 args[2],
													 u8" 已經存在！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"構成 ",
													 args[2],
													 u8" は既に存在します！");
					break;
					std::unreachable();
				}
				return true;
			} else {
				if (configMgr.createConfig(args[2])) {
					switch (SettingManager::languageget) {
					case 0:
					default:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::GREEN,
														 u8"Successfully created config: ",
														 args[2]);
						break;
					case 1:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::GREEN,
														 u8"创建配置 ",
														 args[2],
														 u8" 成功！");
						break;
					case 2:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::GREEN,
														 u8"創建配置 ",
														 args[2],
														 u8" 成功！");
						break;
					case 3:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::GREEN,
														 u8"構成 ",
														 args[2],
														 u8" が正常に作成されました！");
						break;
						std::unreachable();
					}
					return true;
				} else {
					switch (SettingManager::languageget) {
					case 0:
					default:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::RED,
														 u8"Failed to creat config: ",
														 args[2]);
						break;
					case 1:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::RED,
														 u8"创建配置 ",
														 args[2],
														 u8" 失败！");
						break;
					case 2:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::RED,
														 u8"創建配置 ",
														 args[2],
														 u8" 失敗！");
						break;
					case 3:
						C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
														 u8"[Helium] ",
														 Utils::TextColor::RED,
														 u8"構成 ",
														 args[2],
														 u8" 作成に失敗しました！");
						break;
						std::unreachable();
					}
					return true;
				}
				return true;
			}
		} else if (args[1] == u8"delete"sv) {
			if (args[2] == configMgr.currentConfig) {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"Unable to delete the current configuration!");
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"无法删除当前配置！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"無法刪除當前配置！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"現在の構成を削除できませんでした！");
					break;
					std::unreachable();
				}
				return true;
			}
			fast_io::vector<std::u8string> configsString{ConfigManager::list()};
			if (std::ranges::any_of(configsString, [&args](std::u8string& al) { return args[2] == al; }) && configMgr.deleteConfig(args[2])) {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"Successfully deleted config: ",
													 args[2]);
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"删除配置 ",
													 args[2],
													 u8" 成功！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"刪除配置 ",
													 args[2],
													 u8" 成功！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::GREEN,
													 u8"構成 ",
													 args[2],
													 u8" が正常に削除されました！");
					break;
					std::unreachable();
				}
				return true;
			} else {
				switch (SettingManager::languageget) {
				case 0:
				default:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"Failed to delete config: ",
													 args[2]);
					break;
				case 1:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"删除配置 ",
													 args[2],
													 u8" 失败！");
					break;
				case 2:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"刪除配置 ",
													 args[2],
													 u8" 失敗！");
					break;
				case 3:
					C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
													 u8"[Helium] ",
													 Utils::TextColor::RED,
													 u8"構成 ",
													 args[2],
													 u8" 削除に失敗しました！");
					break;
					std::unreachable();
				}
				return true;

			}
		} else {
			return false;
		}
	}
	return false;
}

fast_io::vector<aliasList_struct> configs_ConfigCommand{};

bool listCommand_ConfigCommand(fast_io::vector<std::u8string>*) {
	fast_io::vector<std::u8string> configsString{ConfigManager::list()};

	configs_ConfigCommand.clear();
	configs_ConfigCommand.reserve(configsString.size());
	for (auto& i : configsString)
		configs_ConfigCommand.emplace_back_unchecked(string_Object{std::move(i), nullptr}); // MOVE!!!
	return true;
}

ICommand ConfigCommand{
	u8"config",
	u8".config [load <config>, create <config>, save, delete <config>, list]",
	{u8"Load, Create, Save, Delete or List Configuration", u8"加载， 创建， 保存， 删除或列出配置", u8"加載， 創建， 保存， 刪除或列出配置", u8"構成の読み込み、 作成、 保存、 削除、 一覧表示"},
	{},
	fast_io::vector<aliasList_struct>{
		aliasList_struct{stringview_Judge{u8"load", &configs_ConfigCommand, &listCommand_ConfigCommand}},
		aliasList_struct{stringview_Object{u8"create", nullptr}},
		aliasList_struct{stringview_Object{u8"save", nullptr}},
		aliasList_struct{stringview_Judge{u8"delete", &configs_ConfigCommand, &listCommand_ConfigCommand}},
		aliasList_struct{stringview_Judge{u8"list", nullptr, &listCommand_ConfigCommand}},
	},
	nullptr,
	&ConfigCommand_onExecute};
